<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

// +----------------------------------------------------------------------
// | 消费记录
// +----------------------------------------------------------------------
namespace app\controller;
use think\facade\View;
use think\facade\Db;

class Payorder extends Common
{
    public function initialize(){
		parent::initialize();
		if(bid > 0) showmsg('无访问权限');
	}
    public function index(){
		if(request()->isAjax()){
			$page = input('param.page');
			$limit = input('param.limit');
			if(input('param.field') && input('param.order')){
				$order = 'payorder.'.input('param.field').' '.input('param.order');
			}else{
				$order = 'payorder.id desc';
			}
			$where = array();
			$where[] = ['payorder.aid','=',aid];
			$where[] = ['payorder.status','=',1];
			$where[] = ['payorder.money','>',0];
			if(input('param.paytypeid')) $where[] = ['payorder.paytypeid','=',trim(input('param.paytypeid'))];
			
			if(input('param.nickname')) $where[] = ['member.nickname','like','%'.trim(input('param.nickname')).'%'];
			if(input('param.mid')) $where[] = ['payorder.mid','=',trim(input('param.mid'))];
			if(input('?param.status') && input('param.status')!=='') $where[] = ['payorder.status','=',input('param.status')];
            if(input('param.ctime') ){
                $ctime = explode(' ~ ',input('param.ctime'));
                $where[] = ['payorder.createtime','>=',strtotime($ctime[0])];
                $where[] = ['payorder.createtime','<',strtotime($ctime[1]) + 86400];
            }
			$count = 0 + Db::name('payorder')->alias('payorder')->field('member.nickname,member.headimg,payorder.*')->join('member member','member.id=payorder.mid')->where($where)->count();
			$data = Db::name('payorder')->alias('payorder')->field('member.nickname,member.headimg,payorder.*')->join('member member','member.id=payorder.mid')->where($where)->page($page,$limit)->order($order)->select()->toArray();
			return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
		}
		return View::fetch();
    }
	//导出
	public function excel(){
		if(input('param.field') && input('param.order')){
			$order = 'payorder.'.input('param.field').' '.input('param.order');
		}else{
			$order = 'payorder.id desc';
		}
		$where = array();
		$where[] = ['payorder.aid','=',aid];
		$where[] = ['payorder.status','=',1];
		$where[] = ['payorder.money','>',0];
		if(input('param.paytypeid')) $where[] = ['payorder.paytypeid','=',trim(input('param.paytypeid'))];
		if(input('param.nickname')) $where[] = ['member.nickname','like','%'.trim(input('param.nickname')).'%'];
		if(input('param.mid')) $where[] = ['payorder.mid','=',trim(input('param.mid'))];
		if(input('?param.status') && input('param.status')!=='') $where[] = ['payorder.status','=',input('param.status')];
        if(input('param.ctime') ){
            $ctime = explode(' ~ ',input('param.ctime'));
            $where[] = ['payorder.createtime','>=',strtotime($ctime[0])];
            $where[] = ['payorder.createtime','<',strtotime($ctime[1]) + 86400];
        }
		$list = Db::name('payorder')->alias('payorder')->field('member.nickname,member.headimg,payorder.*')->join('member member','member.id=payorder.mid')->where($where)->order($order)->select()->toArray();
		$title = array();
		$title[] = t('会员').'信息';
		$title[] = '订单号';
		$title[] = '支付项目';
		$title[] = '支付金额';
		$title[] = '支付方式';
        $title[] = '发起时间';
		$title[] = '支付时间';
		$data = array();
		foreach($list as $v){
			$tdata = array();
			$tdata[] = $v['nickname'].'('.t('会员').'ID:'.$v['mid'].')';
			$tdata[] = ' '.$v['ordernum'];
			$tdata[] = $v['title'];
			$tdata[] = $v['money'];
			$tdata[] = $v['paytype'];
            $tdata[] = date('Y-m-d H:i:s',$v['createtime']);
			$tdata[] = date('Y-m-d H:i:s',$v['paytime']);
			$data[] = $tdata;
		}
		$this->export_excel($title,$data);
	}
	//删除
	public function del(){
		$ids = input('post.ids/a');
		Db::name('payorder')->where('aid',aid)->where('id','in',$ids)->delete();
		\app\common\System::plog('消费记录删除'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'删除成功']);
	}
	//头条小程序分账
	public function datasettle(){
		$ids = input('post.ids/a');
		$payorderlist = Db::name('payorder')->where('aid',aid)->where('id','in',$ids)->where('paytypeid',12)->where('status',1)->where('money','>',0)->where('issettle',0)->select()->toArray();
		$successnum = 0;
		foreach($payorderlist as $payorder){
			$rs = \app\common\Ttpay::settle(aid,$payorder['ordernum']);
			if($rs['status'] == 1){
				Db::name('payorder')->where('id',$payorder['id'])->update(['issettle'=>1]);
				$successnum++;
			}else{
				return json(['status'=>0,'msg'=>$rs['msg']]);
			}
		}
		return json(['status'=>1,'msg'=>'成功处理分账'.$successnum.'条']);
	}
}
